package com.neusoft.neuiotms.dcom.service.hn.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.hn.HnInspectionItemConverter;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionItemDTO;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionItem;
import com.neusoft.neuiotms.dcom.repository.hn.HnInspectionItemRepository;
import com.neusoft.neuiotms.dcom.service.hn.HnInspectionItemService;

@Service
public class HnInspectionItemServiceImpl implements HnInspectionItemService {

    @Autowired
    private HnInspectionItemRepository hnInspectionItemRepository;

    @Autowired
    private HnInspectionItemConverter hnInspectionItemConverter;

    @Override
    public List<HnInspectionItemDTO> index(final Searchable searchable) {
        final List<HnInspectionItem> item = this.hnInspectionItemRepository.index(searchable);
        return this.hnInspectionItemConverter.toListDTO(item);
    }

    @Override
    public Page<HnInspectionItemDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<HnInspectionItem> page = this.hnInspectionItemRepository.search(pageable, searchable);
        return this.hnInspectionItemConverter.toPageDTO(page);
    }

    @Override
    public HnInspectionItemDTO create(final HnInspectionItemDTO dto, final UserBean user) {
        if (user == null) {
            throw new UnitedException("当前用户为空！");
        }
        final HnInspectionItem item =
                this.hnInspectionItemRepository.findByCategoryAndItemTypeAndItem(dto.getCategory(), dto.getItemType(),
                        dto.getItem());
        if (item != null) {
            throw new UnitedException("同一分类、类别下请不要创建相同的巡检内容");
        }
        dto.setUserCode(user.getUsername());
        dto.setUserName(user.getName());

        return this.hnInspectionItemConverter
                .toDTO(this.hnInspectionItemRepository.save(this.hnInspectionItemConverter.toModel(dto)));
    }

    @Override
    public HnInspectionItemDTO update(final Long id, final HnInspectionItemDTO dto, final UserBean user) {
        HnInspectionItem model;
        try {
            model = this.hnInspectionItemRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        if (user == null) {
            throw new UnitedException("当前用户为空！");
        }

        final HnInspectionItem item =
                this.hnInspectionItemRepository.findByCategoryAndItemTypeAndItem(dto.getCategory(), dto.getItemType(),
                        dto.getItem());
        if ((item != null) && (item.getId().longValue() != dto.getId().longValue())) {
            throw new UnitedException("同一分类、类别下请不要创建相同的巡检内容", new Object[] {id });
        }
        dto.setUserCode(user.getUsername());
        dto.setUserName(user.getName());
        dto.setUpdateTime(new Date());
        model = this.hnInspectionItemConverter.toModel(dto);
        return this.hnInspectionItemConverter.toDTO(this.hnInspectionItemRepository.save(model));
    }

    @Override
    public HnInspectionItemDTO details(final Long id) {
        HnInspectionItem model;
        try {
            model = this.hnInspectionItemRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.hnInspectionItemConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        HnInspectionItem model;
        try {
            model = this.hnInspectionItemRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        this.hnInspectionItemRepository.delete(model);
    }

}
